package com.biter.service;

import java.util.Date;

import com.biter.MainActivity;
import com.biter.R;
import com.biter.bean.KeyValueBean;
import com.biter.bean.MarketTickerBean;
import com.biter.config.ConfigDef;
import com.biter.util.Formater;
import com.biter.util.Util;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.util.Log;

public class PollingService extends Service {

	public static final String ACTION = "com.biter.service.PollingService";
    private String TAG = "PollingService";
	private Notification  notification;
	private NotificationManager mManager;
	private SharedPreferences setInfo = null;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		initNotifiManager();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		setInfo = getSharedPreferences("setInfo", 0);
		
		new PollingThread().start();
	}

	private void initNotifiManager() {
		mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		notification  = new Notification();
		notification.icon = R.drawable.ic_launcher;
		notification.defaults |= Notification.DEFAULT_SOUND;   
        notification.flags |= Notification.FLAG_AUTO_CANCEL;  
	}

	private void showNotification(String content) {
		notification.when = System.currentTimeMillis(); 
		Intent i = new Intent(this, MainActivity.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i,Intent.FLAG_ACTIVITY_NEW_TASK);
		notification.setLatestEventInfo(this, getResources().getString(R.string.app_name), content, pendingIntent);
		mManager.notify(0, notification);
	}

	class PollingThread extends Thread {
		@Override
		public void run() {
			Intent intent = null;
			MarketTickerBean mtb = null;
			try {
				intent = new Intent();
				KeyValueBean[] valueBeans = ConfigDef.marketList;
				for(KeyValueBean kvBean:valueBeans){
					String jsonData = Util.getHtml(kvBean.getValueAtIdx(1), "utf-8");
					Log.v(TAG, jsonData);
					if(kvBean.getKey() == ConfigDef.CHINA_BITER_ID) mtb = new MarketTickerBean(jsonData);
					intent.putExtra(kvBean.getValueAtIdx(2), jsonData);				
				}		
				intent.setAction("com.biter.marketticker");
				sendBroadcast(intent);
				
				double maxValue = Double.parseDouble(setInfo.getString("maxValue", "0"));
				double minValue = Double.parseDouble(setInfo.getString("minValue", "0"));
								
				double last = Double.parseDouble(mtb.getLast());
				String nowDatesString = Formater.formatDateTime2(new Date());
				
				if(last>maxValue&&maxValue!=0){
					showNotification("市场价格高于预警 "+maxValue+"!目前价格:"+last);
				}else if(last<minValue&&minValue!=0){
					showNotification("市场价格低于预警 "+minValue+"!目前价格:"+last);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

}
